/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.view;

import com.fxexperience.javafx.animation.FlipOutXTransition;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.Transition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import static javafx.application.Application.launch;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Lam
 */
public class Main extends Application {

    private Stage stage = null;

    static {
        try { 

        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");//Windows风格 

//         UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel") ; //Mac风格 

//         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel") ;//Java默认风格 

     } catch (ClassNotFoundException ex) { 

         ex.printStackTrace(); 

     } catch (InstantiationException ex) { 

         ex.printStackTrace(); 

     } catch (IllegalAccessException ex) { 

         ex.printStackTrace(); 

     } catch (UnsupportedLookAndFeelException ex) { 

         ex.printStackTrace(); 

     }
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("AddressBook");
        Parent root = FXMLLoader.load(getClass().getResource("UIFXML.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setResizable(false);
        stage.initStyle(StageStyle.TRANSPARENT);
        scene.getStylesheets().add("com/addressBook/view/style-dark.css");
        stage.getIcons().add(new Image("com/addressBook/view/icons/Live Mail.png"));
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                event.consume();
                cleanAndQuit();
            }
        });

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent event) {
                Stage stage = Main.this.stage;
                if (event.getCode() == KeyCode.ESCAPE) {
                    event.consume();
                    cleanAndQuit();
                }
                if (event.getCode() == KeyCode.F1) {
                    stage.setIconified(true);
                }
            }
        }
        );

        stage.show();
//        new FadeInTransition(root).play();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void cleanAndQuit() {
        Node node = stage.getScene().getRoot();
        Transition tran = new FlipOutXTransition(node);
        tran.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });
        tran.play();
    }
}
